package com.lb.activity.sys.controller;

import com.lb.activity.po.Permission;
import com.lb.activity.po.SessionUser;
import com.lb.activity.po.Tree;
import com.lb.activity.po.User;
import com.lb.activity.sys.service.PermissionService;
import com.lb.activity.sys.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 001030
 */
@RestController
@Slf4j
public class LoginController {


    @Autowired
    private UserService userService;

    @Autowired
    private PermissionService permissionService;

    /**
     * 登陆
     * @param user
     * @return
     */
    @PostMapping("loginIn")
    public ModelAndView loginIn(User user){
        List<User> login = userService.isLogin(user);
        ModelAndView modelAndView = new ModelAndView();
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUserName(),user.getPassword());
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            User userShiro =(User) subject.getPrincipal();
            SessionUser sessionUser = new SessionUser();
            sessionUser.setAdminUser(userShiro);
            Session session = SecurityUtils.getSubject().getSession();
            //放
            session.setAttribute("sessionUser",sessionUser);
            //重定向到showgroup
            log.info("登录成功");
            session.setAttribute("user",login.get(0));
            modelAndView.addObject("user",login.get(0));
            modelAndView.setViewName("redirect:/showGroup");
            return modelAndView;
        } catch (UnknownAccountException e){
            log.info("用户没找到");
            e.printStackTrace();
        } catch (AuthenticationException e){
            log.info("认证失败");
            e.printStackTrace();
        } catch (Exception e){
            log.info("登陆失败====》"+user.getUserName());
            e.printStackTrace();
        }
        //去掉session
        modelAndView.addObject("errorMess","帐号密码不匹配");
        SecurityUtils.getSubject().logout();
        modelAndView.setViewName("login");
        return modelAndView;
    }

    /**
     * 避免重复登录
     * @param request
     * @return
     */
    @GetMapping(value = "/showGroup")
    public ModelAndView showGroup(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
        //跳转到index之前，组装用户的权限
        User sessionUser = (User)request.getSession().getAttribute("user");
        if(sessionUser == null){
            request.getSession().removeAttribute("user");
            modelAndView.setViewName("login");
        }else {
            Long userId=sessionUser.getId();
            List<Tree<Permission>> menus = permissionService.listMenuTree(userId);
            modelAndView.addObject("menus", menus);
            modelAndView.addObject("user",sessionUser);
            modelAndView.setViewName("main");
        }
        return modelAndView;

    }


    /**
     * 第一种方式同步activity 和 用户 角色之前的关系
     */
    @GetMapping("bindMemberShip")
    public void bindMemberShip(){
        userService.bindMemberShip();
    }

    /**
     * 欢迎页面
     * @return
     */
    @GetMapping("welcome")
    public ModelAndView welcome(){
        return new ModelAndView("welcome");
    }

    /**
     * 退出
     * @param request
     * @return
     */
    @GetMapping("logout")
    public ModelAndView logout(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("user");
        return new ModelAndView("login");
    }



}
